<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="java,NIO,Buffer," />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.0" />






<meta name="description" content="1、Buffer通过Java源码可以查看Buffer实现的数据结构为数组。且Java为不同的数据类型提供了不同的Buffer。例如常用的ByteBuffer，CharBuffer。我们来看下Buffer家族的类图关系。 那么Buffer又是如何实现其读取，写入的呢？这里就需要了解Buffer中的几个概念  容量（capacity）：创建Buffer时设置的大小，不可改变。 上界（limit）：缓冲">
<meta name="keywords" content="java,NIO,Buffer">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaNIO之二：Buffer">
<meta property="og:url" content="https://gitee.com/shuhao/2018/05/07/JavaNIO之二：Buffer/index.html">
<meta property="og:site_name" content="Niki">
<meta property="og:description" content="1、Buffer通过Java源码可以查看Buffer实现的数据结构为数组。且Java为不同的数据类型提供了不同的Buffer。例如常用的ByteBuffer，CharBuffer。我们来看下Buffer家族的类图关系。 那么Buffer又是如何实现其读取，写入的呢？这里就需要了解Buffer中的几个概念  容量（capacity）：创建Buffer时设置的大小，不可改变。 上界（limit）：缓冲">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://gitee.com/blog/images/java/nio/buffer/buffer_structure.png">
<meta property="og:image" content="https://gitee.com/blog/images/java/nio/buffer/buffer_1.png">
<meta property="og:updated_time" content="2018-05-28T01:28:08.573Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JavaNIO之二：Buffer">
<meta name="twitter:description" content="1、Buffer通过Java源码可以查看Buffer实现的数据结构为数组。且Java为不同的数据类型提供了不同的Buffer。例如常用的ByteBuffer，CharBuffer。我们来看下Buffer家族的类图关系。 那么Buffer又是如何实现其读取，写入的呢？这里就需要了解Buffer中的几个概念  容量（capacity）：创建Buffer时设置的大小，不可改变。 上界（limit）：缓冲">
<meta name="twitter:image" content="https://gitee.com/blog/images/java/nio/buffer/buffer_structure.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Mist',
    sidebar: {"position":"right","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://gitee.com/shuhao/2018/05/07/JavaNIO之二：Buffer/"/>





  <title> JavaNIO之二：Buffer | Niki </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?cb9f0585e406f878cb2f29c89ad3cdb7";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Niki</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">天道酬勤</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-categories"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/shuhao/blog/2018/05/07/JavaNIO之二：Buffer/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fang ShuHao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/head.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Niki">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                JavaNIO之二：Buffer
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-05-07T08:47:50+08:00">
                2018-05-07
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/java/" itemprop="url" rel="index">
                    <span itemprop="name">java</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="1、Buffer"><a href="#1、Buffer" class="headerlink" title="1、Buffer"></a>1、Buffer</h2><p>通过Java源码可以查看Buffer实现的数据结构为数组。且Java为不同的数据类型提供了不同的Buffer。例如常用的ByteBuffer，CharBuffer。我们来看下Buffer家族的类图关系。<br><img src="/blog/images/java/nio/buffer/buffer_structure.png"></p>
<p>那么Buffer又是如何实现其读取，写入的呢？这里就需要了解Buffer中的几个概念<br><img src="/blog/images/java/nio/buffer/buffer_1.png"></p>
<ol>
<li>容量（capacity）：创建Buffer时设置的大小，不可改变。</li>
<li>上界（limit）：缓冲区中，第一个不能被读写的元素，也就是缓冲区现存元素的计数。</li>
<li>位置（position）：下一个读取或写入元素的位置，由相应的put/get方法更新。</li>
<li>标记（mark）：一个备忘位置，调用mark()则 mark = position；调用reset()则 position = mark<br>这四个属性间遵循如下的关系：<br>0 &lt;= mark &lt;= position &lt;= limit &lt;= capacity</li>
</ol>
<p>为什么buffer要做如上的结构设计呢，我们接下来看下他的api方法就知道。</p>
<h2 id="2、Buffer操作"><a href="#2、Buffer操作" class="headerlink" title="2、Buffer操作"></a>2、Buffer操作</h2><p>我们来看下Buffer的一些API操作与之前的capacity、limit、position、mark有什么关系</p>
<h4 id="2-1、capacity-、position-、limit"><a href="#2-1、capacity-、position-、limit" class="headerlink" title="2.1、capacity()、position()、limit()"></a>2.1、capacity()、position()、limit()</h4><p>这些方法返回缓冲区所设置的capacity大小以及当前position、limit的值</p>
<h4 id="2-2、position-int-newPosition-、limit-int-newLimit"><a href="#2-2、position-int-newPosition-、limit-int-newLimit" class="headerlink" title="2.2、position(int newPosition)、limit(int newLimit)"></a>2.2、position(int newPosition)、limit(int newLimit)</h4><p>这两个方法用来重新设置Bufferposition，以及limit的值</p>
<h4 id="2-3、mark"><a href="#2-3、mark" class="headerlink" title="2.3、mark()"></a>2.3、mark()</h4><p>在此缓冲区位置设置标记<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">public final Buffer mark() &#123;</span><br><span class="line">    mark = position;</span><br><span class="line">    return this;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>可以看出该方法设置mark = position。用来记录下一个需要读取的元素位置，那么这个方法有什么用呢？</p>
<h4 id="2-4、reset"><a href="#2-4、reset" class="headerlink" title="2.4、reset()"></a>2.4、reset()</h4><p>将次缓冲区的位置重置为以前标记的位置吗，此方法<strong>不更改也不丢弃标记的值。</strong><br><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">final</span> Buffer <span class="title">reset</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> m = mark;</span><br><span class="line">    <span class="keyword">if</span> (m &lt; <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> InvalidMarkException();</span><br><span class="line">    position = m;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>可以看出调用该方法，我们可以重复读取Buffer中的数据，重复读取的开始位置，就是mark标记的位置。所以上一个方法mark()就是帮我们标记处下次需要重复读取时的位置。然后通过reset，我们就能从之前标记的位置开始读了。</p>
<h4 id="2-5、clear"><a href="#2-5、clear" class="headerlink" title="2.5、clear()"></a>2.5、clear()</h4><p>将缓冲区清空，将所有的标记返回到初始值<br><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">final</span> Buffer <span class="title">clear</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    position = <span class="number">0</span>;</span><br><span class="line">    limit = capacity;</span><br><span class="line">    mark = -<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="2-6、flip"><a href="#2-6、flip" class="headerlink" title="2.6、flip()"></a>2.6、flip()</h4><p>该方法用于反转缓冲区。就是将position设为0。因为position永远指向下一个读取/插入的元素位置。所以，当Buffer填充了数据后需要读取时，需要先调用flip()方法才能正常读取数据<br><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">final</span> Buffer <span class="title">flip</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">//将limit设为position，则读取的时候只能读取到写入的值，就算缓冲区没有写满，则空余的位置不会读取。（不向IO中read数据到byte数组中时，如果byte没有写满，直接读取byte则会将空余位置读出来）</span></span><br><span class="line">    limit = position;</span><br><span class="line">    position = <span class="number">0</span>;</span><br><span class="line">    mark = -<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="2-7、rewind"><a href="#2-7、rewind" class="headerlink" title="2.7、rewind()"></a>2.7、rewind()</h4><p>在缓冲区读取/写入之前调用该方法，重新读取/写缓冲区。将position设为0， mark设为-1。类似于重置缓冲区<br><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">final</span> Buffer <span class="title">rewind</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    position = <span class="number">0</span>;</span><br><span class="line">    mark = -<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>该方法与flip类似，但是该方法不影响limit的值</strong></p>
<h4 id="2-8、remaining"><a href="#2-8、remaining" class="headerlink" title="2.8、remaining()"></a>2.8、remaining()</h4><p>该方法返回当前位置与限制之间的元素数。 则返回缓冲区还剩多少可读数据<br><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">int</span> <span class="title">remaining</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> limit - position;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="2-9、hasRemaining"><a href="#2-9、hasRemaining" class="headerlink" title="2.9、hasRemaining()"></a>2.9、hasRemaining()</h4><p>这个方法与remaining相似，用于判断缓冲区是否有可读数据<br><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">boolean</span> <span class="title">hasRemaining</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> position &lt; limit;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="2-10、isReadOnly"><a href="#2-10、isReadOnly" class="headerlink" title="2.10、isReadOnly()"></a>2.10、isReadOnly()</h4><p>判断该缓冲区是否为只读。部分缓冲区可设置为只读，不允许修改。</p>
<h4 id="2-11、array"><a href="#2-11、array" class="headerlink" title="2.11、array()"></a>2.11、array()</h4><p>该方法返回缓冲区底层实现数组。</p>
<h2 id="3、Buffer创建"><a href="#3、Buffer创建" class="headerlink" title="3、Buffer创建"></a>3、Buffer创建</h2><p>Buffer的创建有两种方式：allocate,allocateDirect</p>
<h4 id="3-1、allocate"><a href="#3-1、allocate" class="headerlink" title="3.1、allocate"></a>3.1、allocate</h4><p>我们先看下源码<br><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> ByteBuffer <span class="title">allocate</span><span class="params">(<span class="keyword">int</span> capacity)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (capacity &lt; <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException();</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> HeapByteBuffer(capacity, capacity);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">HeapByteBuffer(<span class="keyword">int</span> cap, <span class="keyword">int</span> lim) &#123;            <span class="comment">// package-private</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">super</span>(-<span class="number">1</span>, <span class="number">0</span>, lim, cap, <span class="keyword">new</span> <span class="keyword">byte</span>[cap], <span class="number">0</span>);</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    hb = new byte[cap];</span></span><br><span class="line"><span class="comment">    offset = 0;</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ByteBuffer(<span class="keyword">int</span> mark, <span class="keyword">int</span> pos, <span class="keyword">int</span> lim, <span class="keyword">int</span> cap, <span class="keyword">byte</span>[] hb, <span class="keyword">int</span> offset)&#123;</span><br><span class="line">    <span class="keyword">super</span>(mark, pos, lim, cap);</span><br><span class="line">    <span class="keyword">this</span>.hb = hb;</span><br><span class="line">    <span class="keyword">this</span>.offset = offset;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>可以看出按容量初始化一个byte数组，并且将mark，position，limit，capacity赋初始值。<br>其实例化一个HeapByteBuffer。Heap，堆，java对象不都是在Heap堆上创建的么，这样命名是不是还有不在Heap堆上创建的对象呢？</p>
<h4 id="3-2、-allocateDirect"><a href="#3-2、-allocateDirect" class="headerlink" title="3.2、 allocateDirect"></a>3.2、 allocateDirect</h4><p>该创建方法仅字节缓冲（ByteBuffer）区拥有。该方法创建的是<strong>直接缓冲区</strong><br>为什么要有这个方法呢？</p>
<ol>
<li>因为操作系统进行IO操作的内存区域是连续的内存区域，并且是相连的字节序列。这也是为什么只有ByteBuffer有直接缓冲区。</li>
<li>因为JVM中，数组通过对象存储在<strong>JVM的进程内存空间中</strong>。并且不保证其是连续存储，而且还可能被GC标记移动（GC收集无用对象时，为保证存储空间会移动已存在对象到连续内存空间，以便整理出连续的可用内存空间供下次对象创建）。而且不同的JVM实现不同。</li>
<li>因此为了保证效率，可用将字节缓冲区不创建在JVM进程的内存中，而是创建在C Heap中。这样操作系统可用直接访问该区域的数据，无需通过JVM内存空间再拷贝到系统内存空间进行IO操作。<br>这就是为什么使用直接缓冲区提高IO效率。(减少了复制次数，减少了不连续的JVM进程中的内存空间到连续的系统内存空间的映射操作)并且，我们可以释放申请的直接缓冲区<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"> <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">clear</span><span class="params">(<span class="keyword">final</span> ByteBuffer buffer)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (buffer.isDirect()) &#123;</span><br><span class="line">        ((DirectBuffer) buffer).cleaner().clean();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h4 id="3-2、wrap"><a href="#3-2、wrap" class="headerlink" title="3.2、wrap"></a>3.2、wrap</h4><blockquote>
<blockquote>
<p>参考<br><a href="https://blog.csdn.net/xieyuooo/article/details/7547435" target="_blank" rel="external">DirectByteBuffer</a></p>
</blockquote>
</blockquote>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/java/" rel="tag"># java</a>
          
            <a href="/blog/tags/NIO/" rel="tag"># NIO</a>
          
            <a href="/blog/tags/Buffer/" rel="tag"># Buffer</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/05/07/JavaNIO之一：IO介绍/" rel="next" title="JavaNIO之一：IO介绍">
                <i class="fa fa-chevron-left"></i> JavaNIO之一：IO介绍
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/05/07/PlayFramework之一：搭建开发环境/" rel="prev" title="PlayFramework之一：搭建开发环境">
                PlayFramework之一：搭建开发环境 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNjY5NS8xMzIzMA=="></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/head.jpg"
               alt="Fang ShuHao" />
          <p class="site-author-name" itemprop="name">Fang ShuHao</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives">
                <span class="site-state-item-count">135</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              <a href="/blog/categories">
                <span class="site-state-item-count">35</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/blog/tags">
                <span class="site-state-item-count">93</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1、Buffer"><span class="nav-number">1.</span> <span class="nav-text">1、Buffer</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2、Buffer操作"><span class="nav-number">2.</span> <span class="nav-text">2、Buffer操作</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1、capacity-、position-、limit"><span class="nav-number">2.0.1.</span> <span class="nav-text">2.1、capacity()、position()、limit()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2、position-int-newPosition-、limit-int-newLimit"><span class="nav-number">2.0.2.</span> <span class="nav-text">2.2、position(int newPosition)、limit(int newLimit)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3、mark"><span class="nav-number">2.0.3.</span> <span class="nav-text">2.3、mark()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4、reset"><span class="nav-number">2.0.4.</span> <span class="nav-text">2.4、reset()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5、clear"><span class="nav-number">2.0.5.</span> <span class="nav-text">2.5、clear()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-6、flip"><span class="nav-number">2.0.6.</span> <span class="nav-text">2.6、flip()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-7、rewind"><span class="nav-number">2.0.7.</span> <span class="nav-text">2.7、rewind()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-8、remaining"><span class="nav-number">2.0.8.</span> <span class="nav-text">2.8、remaining()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-9、hasRemaining"><span class="nav-number">2.0.9.</span> <span class="nav-text">2.9、hasRemaining()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-10、isReadOnly"><span class="nav-number">2.0.10.</span> <span class="nav-text">2.10、isReadOnly()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-11、array"><span class="nav-number">2.0.11.</span> <span class="nav-text">2.11、array()</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3、Buffer创建"><span class="nav-number">3.</span> <span class="nav-text">3、Buffer创建</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1、allocate"><span class="nav-number">3.0.1.</span> <span class="nav-text">3.1、allocate</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2、-allocateDirect"><span class="nav-number">3.0.2.</span> <span class="nav-text">3.2、 allocateDirect</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2、wrap"><span class="nav-number">3.0.3.</span> <span class="nav-text">3.2、wrap</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Fang ShuHao</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  



  
  

  

  

  

  


  

</body>
</html>
